
#include "CCQTSceneDirector.h"
#include "QtCore"

#include "properties/peditor_ccnode.h"
#include "properties/pefactory.h"
#include "cocoext/cclabelttf_ext.h"
#include "cocoext/ccparticlesystem_ext.h"

static CCQTSceneDirector  s_sceneDirector;

CCQTSceneDirector * CCQTSceneDirector::sharedSceneDirector()
{
    return &s_sceneDirector;
}

CCScene * CCQTSceneDirector::getCurrentScene()
{
    return CCDirector::sharedDirector()->getRunningScene();
}


void CCQTSceneDirector::onItemClicked( QCocoNode *node )
{
     setCurrentQNode( node);
     PEFactory::shareFactory()->createPE(node->id);
    
}
 


QCocoNode*  CCQTSceneDirector::CurrentQNode()
{
    return m_selQNode;
}

void   CCQTSceneDirector::setCurrentQNode(QCocoNode *node)
{
	m_selQNode = node;
}



CCNode* CCQTSceneDirector::CurrentCNode()
{
	if (m_selQNode == 0) return 0;
	return m_selQNode->m_node;
}

QCocoNode * CCQTSceneDirector::addNode( QCocoNode node , CCNode * parent )
{
	CCNode * cnode;
	CCNode *cNewNode;
	switch(node.id)
	{
    case Coco::T_CCNode :
		{
			cNewNode = CCNode::node();
			
		break;
		}
	case Coco::T_CCLayer :
		{   
			cNewNode = CCLayer ::node();
			break;
		}
	case Coco::T_CCLayerColor:
		{
			cNewNode = CCLayerColor ::layerWithColor(ccc4(255,255,255,122));
			/*((CCLayerColor*)cNewNode)->setColor(cc);*/

			break;
		}
	case Coco::T_CCParticleSystem:
		{
            CCParticleSystem_Ext* temNode = new CCParticleSystem_Ext("myweaponfire.plist");
			cNewNode = temNode;
            break;
		}
    case Coco::T_CCMenuItemFont:
        {
            cNewNode = CCMenuItemFont::itemFromString("text" );
			break;
        }
	case Coco::T_CCMenu:
		{
			cNewNode = CCMenu::menuWithItems(0);

			break;
		}
	case Coco::T_CCSprite:
		{
			cNewNode = CCSprite::spriteWithFile("ball.png");
			break;
		}
	case Coco::T_CCLabelTTF:
		{
			CCLabelTTF_Ext * extNode = new CCLabelTTF_Ext("text" , "Courier New" , 15);
			cNewNode = extNode;
			break;
		}
    case Coco::T_CCMenuItemSprite:
    {
		CCSprite * item_normal = CCSprite::spriteWithFile("ball.png");
		CCSprite * item_selected = CCSprite::spriteWithFile("ball.png");
        cNewNode = CCMenuItemSprite::itemFromNormalSprite(item_normal, item_selected);
        break;
    }
	}
    if (CurrentQNode() ==NULL  ||parent == NULL )
	{
		cnode = getCurrentScene();
	}else
	{
		cnode = parent;
	}
	cnode->addChild(cNewNode);
	//set current node
	QCocoNode * qnode = new QCocoNode;
	qnode->id = node.id;
	qnode->m_node = cNewNode;
	qnode->name = node.name;
	setCurrentQNode(qnode);
	return qnode;
}

void CCQTSceneDirector::removeCurrent()
{
	if (m_selQNode == 0) return ;
	m_selQNode->m_node->removeAllChildrenWithCleanup(false);
	m_selQNode->m_node->removeFromParentAndCleanup(true);
	m_selQNode->m_node = 0;
}

void CCQTSceneDirector::changeCurrent( CCNode * newnode )
{
// 	newnode->setContentSize(m_selQNode->m_node->getContentSize());
// 	newnode->setAnchorPoint(m_selQNode->m_node->getAnchorPoint());
// 	newnode->setPosition(m_selQNode->m_node->getPosition());
// 
	CCNode * parent = m_selQNode->m_node->getParent();
	removeCurrent();
	m_selQNode->m_node = newnode;
	parent->addChild(newnode);
	
}











